package org.zsk.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zsk.cs.common.AppUtils;
import org.zsk.cs.dao.IdentityAuthDao;
import org.zsk.cs.dao.UserDao;
import org.zsk.cs.domain.po.IdentityAuth;
import org.zsk.cs.domain.po.SuggestionBox;
import org.zsk.cs.dao.SuggestionBoxDao;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.SuggestionBoxVo;
import org.zsk.cs.exception.BizException;
import org.zsk.cs.service.SuggestionBoxService;
import org.zsk.cs.util.UserUtils;
import org.zsk.fastdev.assign.Assign;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 意见箱 serviceImpl
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SuggestionBoxServiceImpl extends ServiceImpl<SuggestionBoxDao,SuggestionBox> implements SuggestionBoxService {

    private final SuggestionBoxDao suggestionBoxDao;
    private final IdentityAuthDao identityAuthDao;
    private final UserDao userDao;
    private final Assign assign;
    private final AppUtils appUtils;

    @Autowired
    public SuggestionBoxServiceImpl(SuggestionBoxDao suggestionBoxDao, IdentityAuthDao identityAuthDao, UserDao userDao, Assign assign, AppUtils appUtils) {
        this.suggestionBoxDao = suggestionBoxDao;
        this.identityAuthDao = identityAuthDao;
        this.userDao = userDao;
        this.assign = assign;
        this.appUtils = appUtils;
    }

    @Override
    public boolean addByResident(SuggestionBox suggestionBox) {
        suggestionBox.setResidentId(appUtils.getCurResident().getId());
        long districtHeadId=identityAuthDao.selectDistrictHeadIdByResidentId(suggestionBox.getResidentId());
        suggestionBox.setDistrictHeadId(districtHeadId);
        return 1==suggestionBoxDao.insert(suggestionBox);
    }

    @Override
    public boolean addByAdmin(SuggestionBox suggestionBox) {
        long districtHeadId=identityAuthDao.selectDistrictHeadIdByResidentId(suggestionBox.getResidentId());
        suggestionBox.setDistrictHeadId(districtHeadId);
        return 1==suggestionBoxDao.insert(suggestionBox);
    }

    @Override
    public Page<SuggestionBox> checkByDistrictHead(Long districtHeadId, Page<SuggestionBox> page) {
        return suggestionBoxDao.selectPage(page,new LambdaQueryWrapper<SuggestionBox>()
                .eq(SuggestionBox::getDistrictHeadId,districtHeadId));
    }

    @Override
    public boolean adoptByDistrictHead(SuggestionBox suggestionBox) {
        return suggestionBoxDao.updateById(suggestionBox)==1;
    }

    @Override
    public Page<SuggestionBoxVo> getWithUserNameByPage(SuggestionBoxVo suggestionBoxVo) throws Exception {
        Page<SuggestionBox> page=new Page<>();
        assign.assign(suggestionBoxVo,page,"$current,$size");
        Page<SuggestionBox> suggestionBoxPage=page(page);
        Page<SuggestionBoxVo> suggestionBoxVoPage=new Page<>();
        //合并搜索结果
        List<SuggestionBoxVo> suggestionBoxVoList=suggestionBoxPage.getRecords().stream().collect(LinkedList::new,(suggestionBoxVos, suggestionBox) -> {
            IdentityAuth resident=identityAuthDao.selectById(suggestionBox.getResidentId());
            User user=userDao.selectById(resident.getUid());
            SuggestionBoxVo curSuggestionBoxVo=new SuggestionBoxVo();
            try {
                assign.assign(suggestionBox,curSuggestionBoxVo);
                curSuggestionBoxVo.setUsername(user.getUsername());
                suggestionBoxVos.add(curSuggestionBoxVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },LinkedList::addAll);
        assign.assign(suggestionBoxPage,suggestionBoxVoPage);
        suggestionBoxVoPage.setRecords(suggestionBoxVoList);
        return suggestionBoxVoPage;
    }

    @Override
    public Page<SuggestionBoxVo> getWithUserNameByPageForResident(SuggestionBoxVo suggestionBoxVo) throws Exception {
        IdentityAuth resident = appUtils.getCurResident();
        Page<SuggestionBox> page=new Page<>();
        assign.assign(suggestionBoxVo,page,"$current,$size");
        //获取居民意见箱
        Page<SuggestionBox> suggestionBoxPage=page(page,new LambdaQueryWrapper<SuggestionBox>()
                .eq(SuggestionBox::getResidentId,resident.getId()));
        Page<SuggestionBoxVo> suggestionBoxVoPage=new Page<>();
        //合并搜索结果
        List<SuggestionBoxVo> suggestionBoxVoList=suggestionBoxPage.getRecords()
                .stream().collect(LinkedList::new,(suggestionBoxVos, suggestionBox) -> {
            SuggestionBoxVo curSuggestionBoxVo=new SuggestionBoxVo();
            try {
                assign.assign(suggestionBox,curSuggestionBoxVo);
                curSuggestionBoxVo.setUsername(UserUtils.getCurrentUser().getUsername());
                suggestionBoxVos.add(curSuggestionBoxVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },LinkedList::addAll);
        assign.assign(suggestionBoxPage,suggestionBoxVoPage);
        suggestionBoxVoPage.setRecords(suggestionBoxVoList);
        return suggestionBoxVoPage;
    }
    @Override
    public boolean deleteByIdByResident(long suggestionBoxId){
        return suggestionBoxDao.delete(new LambdaQueryWrapper<SuggestionBox>()
                .eq(SuggestionBox::getId,suggestionBoxId)
                .eq(SuggestionBox::getResidentId,appUtils.getCurResident().getId()))==1;
    }

    @Override
    public Page<SuggestionBoxVo> getWithUserNameByPageForDistrictHead(SuggestionBoxVo suggestionBoxVo) throws Exception {
        IdentityAuth districtHead = appUtils.getDistrictHead();
        Page<SuggestionBox> page=new Page<>();
        assign.assign(suggestionBoxVo,page,"$current,$size");
        //社区长获取社区意见箱
        Page<SuggestionBox> suggestionBoxPage=page(page,new LambdaQueryWrapper<SuggestionBox>()
                .eq(SuggestionBox::getDistrictHeadId,districtHead.getId()));
        Page<SuggestionBoxVo> suggestionBoxVoPage=new Page<>();
        //合并搜索结果
        List<SuggestionBoxVo> suggestionBoxVoList=suggestionBoxPage.getRecords()
                .stream().collect(LinkedList::new,(suggestionBoxVos, suggestionBox) -> {
                    SuggestionBoxVo curSuggestionBoxVo=new SuggestionBoxVo();
                    try {
                        assign.assign(suggestionBox,curSuggestionBoxVo);
                        curSuggestionBoxVo.setUsername(UserUtils.getCurrentUser().getUsername());
                        suggestionBoxVos.add(curSuggestionBoxVo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                },LinkedList::addAll);
        assign.assign(suggestionBoxPage,suggestionBoxVoPage);
        suggestionBoxVoPage.setRecords(suggestionBoxVoList);
        return suggestionBoxVoPage;
    }
}
